let loginBtn = document.querySelector('#btn-login')
loginBtn.addEventListener('click', async function (e) {
    e.preventDefault()
    //获取formData
    let form = document.querySelector('.login-form')
    let { username, password } = serialize(form, { hash: true, empty: true })
    //判断数据是否合规
    if (username.trim().length === 0 || password.trim().length === 0) {
        return showToast('账号或密码不能为空')
    }
    if (username.trim().length < 8 || username.trim().length > 30 || password.trim().length < 6 || password.trim().length > 30) {
        return showToast('账号或密码错误')
    }
    //正确提示正确
    try {
        let res = await axios.post('/login', { username, password })
        console.log(res.data.messages);

        showToast(res.data.message)
        //登录鉴权
        localStorage.setItem('token', res.data.data.token)
        setTimeout(() => {
            location.href = './index.html'
        }, 1000);
    } catch (error) {
        //错误提示系统信息
        showToast(error.response.data.message)
    }
})
